//
//  EmailAuthenticationViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/22.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "EmailAuthenticationViewController.h"

@interface EmailAuthenticationViewController ()
@property (weak, nonatomic) IBOutlet UITextField *emailTF;
@property (weak, nonatomic) IBOutlet UITextField *verificationTF;
@property (nonatomic, strong)NSString *yanzhengStr;
@property (nonatomic, strong) NSTimer *timer;;
@property (weak, nonatomic) IBOutlet UIButton *getCOde;//发送验证码

@end
static NSInteger countDownSeconds;

@implementation EmailAuthenticationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"邮箱认证";
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    //创建定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(getValidateCodeCountDown) userInfo:nil repeats:YES];
    [_timer setFireDate:[NSDate distantFuture]];
}
- (IBAction)yanzheng:(UIButton *)sender {
    if ([@""isEqualToString:_emailTF.text]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入邮箱" delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;
    }else if(![self isValidateEmail:_emailTF.text] ){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确邮箱" delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;
    }
    if (!sender.selected) {
        //设置倒计时时间为60妙
        countDownSeconds = 60;
        _getCOde.tintColor = [UIColor clearColor];
        _getCOde.selected = YES;
        _getCOde.enabled = NO;
        _getCOde.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1.00];
        
        
        //打开定时器
        [_timer setFireDate:[NSDate distantPast]];
    }
    NSDictionary *para = @{
                           @"Email":_emailTF.text,
                           };
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Api.GetEmailName" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@",object);
        self.yanzhengStr = [object objectForKey:@"Data"][0][@"VerificationCode"];
    } withFailureBlock:^(NSError *error) {
        [ToastView showErrorWithStatus:@"网络请求失败"];
          } progress:nil];
}

- (IBAction)clickGetEmail:(UIButton *)sender {
    if ([_emailTF.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入邮箱" delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;
    }
    if (_verificationTF.text != self.yanzhengStr) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证码输入错误!" delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;
    }
    NSString *loginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSDictionary *dict = @{
                           @"Email":self.emailTF.text,
                           @"LoginId":loginId
                           };
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"User.GetEmail" withParaments:dict withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@", object);
        [self.navigationController popViewControllerAnimated:YES];
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@", error);
        [ToastView showErrorWithStatus:@"网络请求失败"];
    } progress:^(float progress) {
        
    }];
    
}
//定时器每隔一秒调用的方法
- (void)getValidateCodeCountDown
{
    if (countDownSeconds >= 0) {
        [_getCOde setTitle:[NSString stringWithFormat:@"剩余%lds",(long)countDownSeconds] forState:UIControlStateNormal];
        countDownSeconds--;
    }else{
        countDownSeconds = 60;
        _getCOde.selected = NO;
        _getCOde.enabled = YES;
//        _getCOde.backgroundColor = [UIColor colorWithRed:0.93 green:0.37 blue:0.31 alpha:1.00];
        [_getCOde setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_timer setFireDate:[NSDate distantFuture]];
    }
}

-(BOOL)isValidateEmail:(NSString *)email

{
    
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];
    
    return [emailTest evaluateWithObject:email];
    
}

@end
